package com.kkm.dao.Imp;

import com.kkm.dao.OrderDao;
import com.kkm.pojo.Order;

import com.kkm.util.JdbcUtils_Druid;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

public class OrderDaoImp implements OrderDao {
    private JdbcTemplate template = new JdbcTemplate(JdbcUtils_Druid.getDataSource());

    @Override
    public List<Order> showOrder(int index, int rows) {
//        System.out.println(index+" "+rows);
        String sql = "SELECT order.id,order.oid,order.total,orderflag.orderflag,order.ordertime,order.username,order.address " +
                "FROM `order` ,orderflag " +
                "where `order`.flag=orderflag.flag" +
                " limit ?,? ";
        return template.query(sql, new BeanPropertyRowMapper<>(Order.class), index, rows);
    }

    @Override
    public int checkOrderCount() {

        String sql = "select count(*)  from `order` ,orderflag where `order`.flag=orderflag.flag";
        return template.queryForObject(sql, int.class);
    }

    @Override
    public int checkOrderNum(Map<String, String[]> condition) {
        try {

            String sql = "select count(*) from `order`,orderflag  where  `order`.flag=orderflag.flag and 1=1";
            //解析map  1.对sql拼接
            StringBuffer sb = new StringBuffer(sql);
            //2.获取键值，name address，email
            Set<String> keySet = condition.keySet();
            //3.定义参数集合
            List<Object> params = new ArrayList<Object>();
            //遍历看那个不为空拼接到sql语句中
            //key：name，address，email
            for (String key : keySet) {
                //排除分页条件参数
                if ("currentPage".equals(key) || "rows".equals(key) || "method".equals(key)) {
                    continue;
                }
                String value = condition.get(key)[0];//获取值
                if (key.equals("orderflag")) {
                    if (value != null || "".equals(value)) {
                        //有值
                        sb.append(" and orderflag." + key + " like ? ");
                        params.add("%%" + value + "%");
                    }
                }
                if (key.equals("username")) {
                    if (value != null || "".equals(value)) {
                        //有值
                        sb.append(" and `order`." + key + " like ? ");
                        params.add("%%" + value + "%");
                    }
                }
            }
            return template.queryForObject(sb.toString(), int.class, params.toArray());
        } catch (Exception e) {
            return 0;
        }
    }


    @Override
    public List<Order> checkOrder(Map<String, String[]> condition, int index, int rows) {
        try {
//            String sql="select * from `order` ,orderflag  where  `order`.flag=orderflag.flag and orderflag.orderflag=? and `order`.username=? ";
            String sql = "select * from `order`,orderflag  where  `order`.flag=orderflag.flag and 1=1";
            //解析map  1.对sql拼接
            StringBuffer sb = new StringBuffer(sql);
            //2.获取键值，name address，email
            Set<String> keySet = condition.keySet();
            //3.定义参数集合
            List<Object> params = new ArrayList<Object>();
            //遍历看那个不为空拼接到sql语句中
            //key：name，address，email
            for (String key : keySet) {
                //排除分页条件参数
                if ("currentPage".equals(key) || "rows".equals(key) || "method".equals(key)) {
                    continue;
                }
                String value = condition.get(key)[0];//获取值
                if (key.equals("orderflag")) {
                    if (!"".equals(value)) {
                        //有值
                        sb.append(" and orderflag." + key + " like ? ");
                        params.add("%%" + value + "%");
                    }
                }
                if (key.equals("username")) {
                    if (value != null || "".equals(value)) {
                        //有值
                        sb.append(" and `order`." + key + " like ? ");
                        params.add("%%" + value + "%");
                    }
                }
            }
            sb.append(" limit ?,? ");
            params.add(index);
            params.add(rows);
            sql = sb.toString();
            System.out.println(sql);
            return template.query(sql, new BeanPropertyRowMapper(Order.class), params.toArray());
        } catch (Exception e) {
            return null;
        }
    }


    @Override
    public void deleteOrder(int oid) {
        String sql = "DELETE from `order` where oid = ? ";
        template.update(sql, oid);
    }

    @Override
    public Order orderById(int oid) {
        String sql="SELECT * from `order` where oid = ?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<Order>(Order.class),oid);
    }

    @Override
    public void modifyOrder( String address,int oid) {
        String sql="UPDATE `order` SET  address = ? WHERE oid=?";
        template.update(sql,address,oid);

    }

}
